package org.csu.zhangdian.persistence;

import org.csu.zhangdian.domain.LevelRule;
import org.csu.zhangdian.dao.LevelRuleDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;

@Repository
public class LevelRuleDAOImpl implements LevelRuleDAO {

    @Autowired
    private DataSource dataSource;

    @Override
    public ArrayList<LevelRule> findAll() {
        ArrayList<LevelRule> ruleList=new ArrayList<>();

        try {
            Connection connection=dataSource.getConnection();
            Statement statement=connection.createStatement();
            ResultSet resultSet=statement.executeQuery(FIND_ALL);

            while (resultSet.next()){
                LevelRule temp=new LevelRule();
                temp.setLevel(resultSet.getInt("level"));
                temp.setName(resultSet.getString("discription"));
                temp.setPoint(resultSet.getInt("point"));
                ruleList.add(temp);
            }
            resultSet.close();
            statement.close();
            connection.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ruleList;
    }
}


//    @Override
//    public LevelRule getLevelRule(LevelRule levelRule) {
//        try {
//
//            Connection connection=dataSource.getConnection();
//            PreparedStatement preparedStatement = connection.prepareStatement(GET_LEVEL_RULE);
//
//            preparedStatement.setInt(1,levelRule.getLevel());
//            preparedStatement.setString(2,levelRule.getName());
//            preparedStatement.setInt(3,levelRule.getPoint());
//
//            ResultSet resultSet=preparedStatement.executeQuery();
//
//            if (resultSet.next()){
//                levelRule=new LevelRule();
//                levelRule.setLevel(resultSet.getInt("id"));
//                levelRule.setName(resultSet.getString("name"));
//                levelRule.setPoint(resultSet.getInt("point"));
//            }
//            resultSet.close();
//            preparedStatement.close();
//            connection.close();
//
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//
//        return levelRule;
//    }